package sync;

import java.util.Vector;

class VideoShop2 {
	private Vector buffer = new Vector();
	public VideoShop2() {
		buffer.addElement("은하철도999-0");
		buffer.addElement("은하철도999-1");
		buffer.addElement("은하철도999-2");
		buffer.addElement("은하철도999-3");
	}
	public synchronized String lendVideo() {
		if (this.buffer.size() > 0) {
			String v = (String) this.buffer.remove(buffer.size() - 1);
			return v;
		} else {
			return null;
		}
	}
	public synchronized void returnVideo(String video) {
		this.buffer.addElement(video);
	}
}

class Person2 extends Thread {
	public void run() {
		try {
			String v = VideoShopMain2.vShop.lendVideo();	// 비디오 빌림
			if (v != null) {
				System.out.println(this.getName() + ": " + v + " 대여");
				System.out.println(this.getName() + ": " + v + " 보는 중");
				Thread.sleep(5000);
				System.out.println(this.getName() + ": " + v + " 반납");
				VideoShopMain2.vShop.returnVideo(v);
			} else {
				System.out.println(this.getName() + ": 비디오가 없네.. 못보겠네..");
			}
		} catch (InterruptedException e) {e.printStackTrace();}
	}
}

public class VideoShopMain2 {
	public static VideoShop2 vShop = new VideoShop2();
	public static void main(String[] args) {
		System.out.println("프로그램 시작");
		Person2 p1 = new Person2();
		Person2 p2 = new Person2();
		Person2 p3 = new Person2();
		Person2 p4 = new Person2();
		Person2 p5 = new Person2();
		p1.start();
		p2.start();
		p3.start();
		p4.start();
		p5.start();
		System.out.println("프로그램 종료");
	}
}
